package com.hsq.spzx.common.exception;

import com.hsq.spzx.model.vo.common.Result;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.method.annotation.MethodArgumentTypeMismatchException;

/**
 * @author hsq
 * @date 2025-05-28 22:21
 */
@ControllerAdvice
public class GlobalExceptionHandler {

    @ExceptionHandler(Exception.class)
    public Result error(Exception e) {
        e.printStackTrace();
        return Result.build(null, 201, "出现了异常");
    }

    @ExceptionHandler(HsqException.class)
    public Result error(HsqException exception) {
        exception.printStackTrace();
        return Result.build(null, exception.getResultCodeEnum());
    }

    @ExceptionHandler(MethodArgumentTypeMismatchException.class)
    @ResponseStatus(HttpStatus.BAD_REQUEST)
    public Result handleTypeMismatch(MethodArgumentTypeMismatchException ex) {
//        return new ErrorResponse(
//                "参数类型错误",
//                ex.getMessage(),
//                System.currentTimeMillis()
//        );
        return Result.build(null, 204, ex.getMessage());
    }
}
